<%--
  Created by IntelliJ IDEA.
  User: GZ
  Date: 2021/7/10
  Time: 14:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>视频分类查询</title>
</head>
<body>
<form action="/sort/query" method="post">
    <h3>分类查询</h3>
    <label><input type="radio" name="level" value="1" ${1==param.level?'checked':''}>一级分类</label>
    <label><input type="radio" name="level" value="2" ${2==param.level?'checked':''}>二级分类</label>
    <label><input type="radio" name="level" value="0" ${param.level==null or 0==param.level?'checked':''}>不限</label>
    <button>查询</button>
</form>

<table>
    <thead>
    <tr>
        <th>序号</th>
        <th>分类名称</th>
        <th>等级</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:choose>
        <c:when test="${ empty requestScope.sortList }">
            <tr>
                <td colspan="4" style="text-align: center;color: red">
                    未查询到任何数据
                </td>
            </tr>
        </c:when>
        <c:otherwise>
            <script src="/js/jquery/jquery-3.3.1.js"></script>
            <script>
                /**
                 * 根据ID删除分类
                 * @param id ID
                 * @param type 分类名称
                 */
                function removeSort(id, type) {
                    if (!window.confirm('确定要删除视频分类【' + type + '】吗?')) {
                        //取消
                        return;
                    }
                    //见jquery帮助文档
                    $.ajax({
                        url: '/sort/remove',
                        type: 'get',
                        data: {'id': id},
                        dataType: 'json',
                        success: function (data) {
                            if (1 == data) {
                                alert('删除成功!!');
                                //刷新页面
                                window.location.reload(true);
                            } else if (0 == data) {
                                alert('该视频分类不存在或已被删除');
                            } else {
                                alert('删除视频分类,服务器返回未知的状态!!');
                            }
                        },
                        error: function () {

                        }
                    });
                }
            </script>
            <c:forEach items="${requestScope.sortList}" var="sort" varStatus="s">
                <tr>
                    <td>${s.count}</td>
                    <td>${sort.type}</td>
                    <td>${1==sort.level?'一级':(2==sort.level?'二级':'未知')}</td>
                    <td>
                        <button type="button" onclick="removeSort(${sort.id},'${sort.type}')">删除</button>
                        <button type="button" onclick="window.open('/sort/modify_sort.jsp?id=${sort.id}','modifySort')">修改</button>
                    </td>
                </tr>
            </c:forEach>
        </c:otherwise>
    </c:choose>
    </tbody>

</table>
</body>
</html>
